{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# SimpleByzerLLM 测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from byzerllm.utils.client.simple_byzerllm_client import SimpleByzerLLM\n",
    "\n",
    "# 初始化客户端\n",
    "llm = SimpleByzerLLM(default_model_name=\"deepseek_chat\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 部署模型\n",
    "deploy_result = llm.deploy(\n",
    "    model_path=\"\",\n",
    "    pretrained_model_type=\"saas/reasoning_openai\",\n",
    "    udf_name=\"o_deepseek_chat\",\n",
    "    infer_params={\n",
    "        \"saas.base_url\": \"https://api.deepseek.com/v1\",\n",
    "        \"saas.api_key\": os.getenv(\"MODEL_DEEPSEEK_TOKEN\"),\n",
    "        \"saas.model\": \"deepseek-reasoner\",\n",
    "        \"saas.is_reasoning\": True\n",
    "    }\n",
    ")\n",
    "deploy_result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 部署模型\n",
    "deploy_result = llm.deploy(\n",
    "    model_path=\"\",\n",
    "    pretrained_model_type=\"saas/reasoning_openai\",\n",
    "    udf_name=\"deepseek_chat\",\n",
    "    infer_params={\n",
    "        \"saas.base_url\": \"http://127.0.0.1:8000/v1\",\n",
    "        \"saas.api_key\": \"xxx\",\n",
    "        \"saas.model\": \"deepseek_r1_chat\",\n",
    "        \"saas.is_reasoning\": True\n",
    "    }\n",
    ")\n",
    "deploy_result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 获取模型元数据\n",
    "meta = llm.get_meta(model=\"deepseek_chat\")\n",
    "meta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 测试普通聊天\n",
    "response = llm.chat_oai(\n",
    "    conversations=[{\"role\": \"user\", \"content\": \"帮我写一个排序算法\"}],\n",
    "    model=\"o_deepseek_chat\"\n",
    ")\n",
    "print(response[0].output)\n",
    "print(\"=\"*100)\n",
    "print(response[0].metadata.get(\"reasoning_content\",\"==\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 测试普通聊天\n",
    "response = llm.chat_oai(\n",
    "    conversations=[{\"role\": \"user\", \"content\": \"帮我写一个排序算法\"}],\n",
    "    model=\"deepseek_chat\"\n",
    ")\n",
    "print(response[0].output)\n",
    "print(\"=\"*100)\n",
    "print(response[0].metadata.get(\"reasoning_content\",\"==\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 测试流式聊天\n",
    "for chunk in llm.stream_chat_oai(\n",
    "    conversations=[{\"role\": \"user\", \"content\": \"写一个Python的快速排序算法\"}],\n",
    "    delta_mode=True,\n",
    "    model=\"o_deepseek_chat\"\n",
    "):\n",
    "    print(chunk[0], end=\"\", flush=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 测试prompt装饰器\n",
    "@llm.prompt(model=\"deepseek_chat\")\n",
    "def generate_story(theme: str, length: int) -> str:\n",
    "    \"\"\"\n",
    "    根据主题生成一个故事\n",
    "    主题：{{theme}}\n",
    "    长度：{{length}}\n",
    "    \"\"\"\n",
    "\n",
    "story = generate_story(theme=\"科幻\", length=200)\n",
    "print(story)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import byzerllm\n",
    "@byzerllm.prompt()\n",
    "def generate_story(theme: str, length: int) -> str:\n",
    "    \"\"\"\n",
    "    根据主题生成一个故事\n",
    "    主题：{{theme}}\n",
    "    长度：{{length}}\n",
    "    \"\"\"\n",
    "\n",
    "story = generate_story.with_llm(llm).run(theme=\"科幻\", length=200)\n",
    "print(story)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 清理模型\n",
    "llm.undeploy(\"test_model\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai\n",
    "client = openai.OpenAI(base_url=\"https://api.deepseek.com/v1\",api_key=os.getenv(\"MODEL_DEEPSEEK_TOKEN\"))\n",
    "llm_config = {}\n",
    "response = client.chat.completions.create(\n",
    "            messages=[{\"role\": \"user\", \"content\": \"帮我写一个排序算法\"}],\n",
    "            model=\"deepseek-reasoner\",\n",
    "            temperature=llm_config.get(\"temperature\", 0.7),\n",
    "            max_tokens=llm_config.get(\"max_tokens\", 4096),\n",
    "            top_p=llm_config.get(\"top_p\", 0.9),            \n",
    "        )\n",
    "generated_text = response.choices[0].message.content\n",
    "print(generated_text)\n",
    "print(\"=\"*100)\n",
    "reasoning_text = \"\"\n",
    "if hasattr(response.choices[0].message, \"reasoning_content\"):\n",
    "    reasoning_text = response.choices[0].message.reasoning_content or \"\"\n",
    "print(reasoning_text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai\n",
    "client = openai.OpenAI(base_url=\"http://127.0.0.1:8000/v1\",api_key=os.getenv(\"MODEL_DEEPSEEK_TOKEN\"))\n",
    "llm_config = {}\n",
    "response = client.chat.completions.create(\n",
    "            messages=[{\"role\": \"user\", \"content\": \"帮我写一个排序算法\"}],\n",
    "            model=\"deepseek_r1_chat\",\n",
    "            temperature=llm_config.get(\"temperature\", 0.7),\n",
    "            max_tokens=llm_config.get(\"max_tokens\", 4096),\n",
    "            top_p=llm_config.get(\"top_p\", 0.9),            \n",
    "        )\n",
    "generated_text = response.choices[0].message.content\n",
    "print(generated_text)\n",
    "print(\"=\"*100)\n",
    "reasoning_text = \"\"\n",
    "if hasattr(response.choices[0].message, \"reasoning_content\"):\n",
    "    reasoning_text = response.choices[0].message.reasoning_content or \"\"\n",
    "print(reasoning_text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai\n",
    "client = openai.OpenAI(base_url=\"https://api.deepseek.com/v1\",api_key=os.getenv(\"MODEL_DEEPSEEK_TOKEN\"))\n",
    "llm_config = {}\n",
    "response = client.chat.completions.create(\n",
    "            messages=[{\"role\": \"user\", \"content\": \"帮我写一个排序算法\"}],\n",
    "            model=\"deepseek-reasoner\",\n",
    "            temperature=llm_config.get(\"temperature\", 0.7),\n",
    "            max_tokens=llm_config.get(\"max_tokens\", 4096),\n",
    "            top_p=llm_config.get(\"top_p\", 0.9),  \n",
    "            stream=True,\n",
    "        )\n",
    "is_content = False\n",
    "for chunk in response:\n",
    "    if not is_content:\n",
    "        print(chunk.choices[0].delta.reasoning_content, end=\"\", flush=True)\n",
    "    if chunk.choices[0].delta.content and not is_content:\n",
    "        is_content = True\n",
    "    if is_content:\n",
    "        print(chunk.choices[0].delta.content, end=\"\", flush=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "嗯，用户让我帮他写一个排序算法。首先，我需要确定他的需求是什么。排序算法有很多种，比如冒泡排序、快速排序、归并排序等等。每种算法的时间复杂度和空间复杂度不同，适用场景也不一样。用户没有具体说明需要哪种类型的排序算法，所以可能需要进一步询问。不过可能他可能只需要一个基础的例子，或者他可能对算法不太熟悉，想了解常见的实现。\n",
      "\n",
      "接下来，我需要考虑用户的身份。他可能是学生，正在学习编程，需要完成一个作业或者练习。也可能是刚开始学习算法的开发者，想了解如何实现排序。这种情况下，选择一个简单易懂的算法比较合适，比如冒泡排序或者选择排序。虽然这些算法效率不高，但容易理解，适合教学。\n",
      "\n",
      "或者，用户可能需要一个高效的排序算法，比如快速排序或归并排序。不过这些算法实现起来稍微复杂一些，特别是快速排序的分区过程，对于新手来说可能有点难度。如果用户没有特别说明，可能默认先给出一个简单实现的例子，再提供更高效的选项。\n",
      "\n",
      "另外，用户可能不知道不同排序算法的区别，这时候我应该解释一下不同算法的特点，帮助他根据需求选择。比如，如果数据量小，简单的排序算法就可以了；如果数据量大，可能需要更高效的算法。或者，是否需要稳定排序（即相同元素的相对顺序不变），比如归并排序是稳定的，而快速排序不稳定。\n",
      "\n",
      "也有可能用户对某种编程语言有特定需求，比如Python、Java或JavaScript。不过问题中没有提到，所以可能需要用通用的伪代码，或者选择一种常见的语言，比如Python，因为其语法简洁易懂，适合示例。\n",
      "\n",
      "假设用户需要一个基础的例子，我可以先给出一个冒泡排序的实现，然后解释其原理和效率。同时，也可以提供更高效的算法，比如快速排序，并说明两者的区别，让用户可以根据自己的情况选择。\n",
      "\n",
      "不过，用户可能只想要一个简单的答案，所以直接给出代码示例，并简要说明。但为了全面，最好覆盖不同的可能性，这样用户可以根据需要调整。\n",
      "\n",
      "另外，还要注意代码的正确性，不能有错误，特别是边界条件，比如空数组、重复元素的情况。确保示例代码能够正确处理这些情况。\n",
      "\n",
      "总结一下，可能的回答结构是：首先询问是否需要特定类型的排序算法，或者直接给出一个简单实现（如冒泡排序），然后介绍其时间和空间复杂度，再提供一个更高效的算法（如快速排序），并比较两者的优缺点。这样既满足了用户的直接需求，又提供了额外信息帮助他做出选择。以下是一个简单的 **快速排序** 算法实现（基于Python），它是一种高效的分治算法，平均时间复杂度为 \\(O(n \\log n)\\)：\n",
      "\n",
      "```python\n",
      "def quick_sort(arr):\n",
      "    if len(arr) <= 1:\n",
      "        return arr\n",
      "    pivot = arr[len(arr) // 2]  # 选择中间元素作为基准值\n",
      "    left = [x for x in arr if x < pivot]\n",
      "    middle = [x for x in arr if x == pivot]\n",
      "    right = [x for x in arr if x > pivot]\n",
      "    return quick_sort(left) + middle + quick_sort(right)\n",
      "\n",
      "# 示例用法\n",
      "arr = [3, 6, 8, 10, 1, 2, 1]\n",
      "sorted_arr = quick_sort(arr)\n",
      "print(sorted_arr)  # 输出: [1, 1, 2, 3, 6, 8, 10]\n",
      "```\n",
      "\n",
      "---\n",
      "\n",
      "### 其他常见排序算法（可根据需求选择）：\n",
      "1. **冒泡排序**（简单但较慢，\\(O(n^2)\\)）：\n",
      "   ```python\n",
      "   def bubble_sort(arr):\n",
      "       n = len(arr)\n",
      "       for i in range(n):\n",
      "           for j in range(0, n-i-1):\n",
      "               if arr[j] > arr[j+1]:\n",
      "                   arr[j], arr[j+1] = arr[j+1], arr[j]\n",
      "       return arr\n",
      "   ```\n",
      "\n",
      "2. **归并排序**（稳定且高效，\\(O(n \\log n)\\)）：\n",
      "   ```python\n",
      "   def merge_sort(arr):\n",
      "       if len(arr) <= 1:\n",
      "           return arr\n",
      "       mid = len(arr) // 2\n",
      "       left = merge_sort(arr[:mid])\n",
      "       right = merge_sort(arr[mid:])\n",
      "       return merge(left, right)\n",
      "   \n",
      "   def merge(left, right):\n",
      "       result = []\n",
      "       i = j = 0\n",
      "       while i < len(left) and j < len(right):\n",
      "           if left[i] < right[j]:\n",
      "               result.append(left[i])\n",
      "               i += 1\n",
      "           else:\n",
      "               result.append(right[j])\n",
      "               j += 1\n",
      "       result += left[i:]\n",
      "       result += right[j:]\n",
      "       return result\n",
      "   ```\n",
      "\n",
      "---\n",
      "\n",
      "### 如何选择？\n",
      "- **数据量小** → 冒泡排序、插入排序（简单易实现）。\n",
      "- **数据量大且需要稳定排序** → 归并排序。\n",
      "- **通用高效场景** → 快速排序（注意最差情况时间复杂度为 \\(O(n^2)\\)，但实践中优化后极快）。\n",
      "\n",
      "如果需要其他语言（如Java/JavaScript）或更详细的解释，请告诉我！"
     ]
    }
   ],
   "source": [
    "import openai\n",
    "import os\n",
    "client = openai.OpenAI(base_url=\"http://127.0.0.1:8000/v1\",api_key=os.getenv(\"MODEL_DEEPSEEK_TOKEN\"))\n",
    "llm_config = {}\n",
    "response = client.chat.completions.create(\n",
    "            messages=[{\"role\": \"user\", \"content\": \"帮我写一个排序算法\"}],\n",
    "            model=\"deepseek_r1_chat\",\n",
    "            temperature=llm_config.get(\"temperature\", 0.7),\n",
    "            max_tokens=llm_config.get(\"max_tokens\", 4096),\n",
    "            top_p=llm_config.get(\"top_p\", 0.9),  \n",
    "            stream=True,\n",
    "        )\n",
    "is_content = False\n",
    "for chunk in response:\n",
    "    if not is_content:\n",
    "        print(chunk.choices[0].delta.reasoning_content, end=\"\", flush=True)\n",
    "    if chunk.choices[0].delta.content and not is_content:\n",
    "        is_content = True\n",
    "    if is_content:\n",
    "        print(chunk.choices[0].delta.content, end=\"\", flush=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import byzerllm\n",
    "llm = byzerllm.ByzerLLM.from_default_model(\"deepseek_r1_chat\")\n",
    "\n",
    "v = llm.stream_chat_oai(\n",
    "    conversations=[{\"role\": \"user\", \"content\": \"帮我写一个排序算法\"}],\n",
    "    model=\"deepseek_r1_chat\",\n",
    "    delta_mode=True\n",
    ")\n",
    "\n",
    "is_content = False\n",
    "for chunk in v:\n",
    "    metadata = chunk[1]\n",
    "    text = chunk[0]\n",
    "    if not is_content:\n",
    "        print(metadata.reasoning_content, end=\"\", flush=True)\n",
    "    if text and not is_content:\n",
    "        is_content = True\n",
    "        print(\"=========\")\n",
    "    if is_content:\n",
    "        print(text, end=\"\", flush=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
